package wpwork.controller;

import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wpwork.base.ActionResult;
import wpwork.base.controller.SuperController;
import wpwork.entity.KshGroupEntity;
import wpwork.model.GroupOrPersonDTO;
import wpwork.model.GroupUserTreeVO;
import wpwork.model.PersonByGroupDTO;
import wpwork.permission.service.UserRelationService;
import wpwork.permission.service.UserService;
import wpwork.service.KshGroupService;
import wpwork.util.JsonUtil;

import javax.validation.Valid;
import java.util.List;

@RestController
    @RequestMapping("/api/groups")
public class KshGroupController extends SuperController<KshGroupService, KshGroupEntity> {

    @Autowired
    private KshGroupService groupService;

    @GetMapping("/tree")
    public ActionResult getGroupTree() {
        try {
            List<KshGroupEntity> groupTree = groupService.getGroupTree();
            return ActionResult.success(groupTree);
        } catch (Exception e) {
            return ActionResult.fail("获取分组树失败: " + e.getMessage());
        }
    }

    @PostMapping
    public ActionResult create(@RequestBody @Valid KshGroupEntity groupEntity) {
        KshGroupEntity entity = JsonUtil.getJsonToBean(groupEntity, KshGroupEntity.class);
        return groupService.createGroup(entity);
    }

    @PutMapping("/{id}")
    public ActionResult update(@PathVariable Long id, @RequestBody @Valid KshGroupEntity groupEntity) {
        groupEntity.setId(id);
        return groupService.updateGroup(groupEntity);
    }

    @DeleteMapping("/{id}")
    public ActionResult delete(@PathVariable Long id) {
        return groupService.deleteGroup(id);
    }

    @PostMapping("/{groupId}/users/bind")
    public ActionResult bindUsers(@PathVariable Long groupId, @RequestBody List<String> userIds) {
        return groupService.bindUsersToGroup(groupId, userIds);
    }

    @PostMapping("/{groupId}/users/unbind")
    public ActionResult unbindUsers(@PathVariable Long groupId, @RequestBody List<String> userIds) {
        return groupService.unbindUsersFromGroup(groupId, userIds);
    }

    @GetMapping("/{groupId}/users")
    public ActionResult getGroupUsers(@PathVariable Long groupId) {
        try {
            List<String> userIds = groupService.getGroupUserIds(groupId);
            return ActionResult.success(userIds);
        } catch (Exception e) {
            return ActionResult.fail("获取分组用户失败: " + e.getMessage());
        }
    }

    @GetMapping("/user/{userId}/groups")
    public ActionResult getUserGroups(@PathVariable String userId) {
        try {
            List<Long> groupIds = groupService.getUserGroupIds(userId);
            return ActionResult.success(groupIds);
        } catch (Exception e) {
            return ActionResult.fail("获取用户分组失败: " + e.getMessage());
        }
    }

    @Autowired
    private UserService userService;

    @Autowired
    private UserRelationService userRelationService;

    /**
     * 获取分组树及每个分组下的用户详情
     */
    @GetMapping("/tree-with-users")
    public ActionResult getGroupTreeWithUsers() {
        try {
            List<GroupUserTreeVO> groupTree = groupService.getGroupTreeWithUsers();
            return ActionResult.success(groupTree);
        } catch (Exception e) {
            return ActionResult.fail("获取分组树及用户失败: " + e.getMessage());
        }
    }

    @GetMapping("/groups-persons")
    public ActionResult getGroupAndPersonList() {
        try {
            List<GroupOrPersonDTO> groupAndPersonList = groupService.getGroupAndPersonList();
            return ActionResult.success(groupAndPersonList);
        } catch (Exception e) {
            return ActionResult.fail("获取分组及用户失败: " + e.getMessage());
        }
    }

    @GetMapping("/person-groups")
    public ActionResult getPersonAndGroupList() {
        try {
            List<PersonByGroupDTO> personAndGroupList = groupService.selectPersonAndGroupName();
            return ActionResult.success(personAndGroupList);
        } catch (Exception e) {
            return ActionResult.fail("获取用户及分组失败: " + e.getMessage());
        }
    }

    @Operation(summary = "删除")
    @PostMapping("/deleteDataOne")
    public ActionResult<?> deleteDataOne(@RequestParam String id) {
        boolean b = groupService.removeById(id);
        if (b){
            return ActionResult.success("删除成功");
        }else{
            return ActionResult.fail("删除失败");
        }
    }



}